package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.HealthItemMapper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import org.junit.internal.Classes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.ArrayList;

import java.util.List;



@Service(interfaceClass = HealthItemService.class)
public class HealthItemServiceImpl implements HealthItemService {


    @Autowired
    private HealthItemMapper healthItemMapper;
    @Override
    public PageResult findPageItem(QueryPageBean queryPageBean) {

//        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
//
//        ArrayList<CheckItem> pageItem = healthItemMapper.findPageItem();
//        int size = pageItem.size();
//
//        PageInfo<CheckItem> pageInfo=new PageInfo<CheckItem>(pageItem);
//        long total = pageInfo.getTotal();
//
//        PageResult pageResult=new PageResult(total,pageItem);

        Page<CheckItem> page = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize())
                .doSelectPage(
                () -> healthItemMapper.findPageItem(queryPageBean.getQueryString())
        );


        PageResult pageResult=new PageResult(page.getTotal(),page.getResult());
        return pageResult;
    }

    @Override
    public Result findByid(int id) {

        CheckItem byId = healthItemMapper.findById(id);

        if (byId==null){
            return new Result (false, MessageConstant.QUERY_CHECKITEM_FAIL,null);
        }

        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,byId);

    }

    @Override
    public Result update(CheckItem checkItems) {
        boolean update = healthItemMapper.update(checkItems);
        if (!update){
            return new Result(update,MessageConstant.EDIT_CHECKITEM_FAIL,null);
        }

        CheckItem data = healthItemMapper.findById(checkItems.getId());


        data.setAge(checkItems.getAge());
        data.setAttention(checkItems.getAttention());
        data.setCode(checkItems.getCode());
        data.setName(checkItems.getName());
        data.setPrice(checkItems.getPrice());
        data.setSex(checkItems.getSex());
        data.setType(checkItems.getSex());
        data.setRemark(checkItems.getRemark());


        return new Result(update,MessageConstant.EDIT_CHECKITEM_SUCCESS,data);
    }

    @Override
    public  Result findAll() {

        List<CheckItem> all = healthItemMapper.findAll();

        if (null == all){
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL,all);
        }

        return  new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,all);
    }

}
